<?php
namespace api\home\controller;

use cmf\controller\RestUserBaseController;
use app\common\model\MytaskModel;
use app\common\model\RecordModel;
use app\common\model\RelationStudentModel;
use app\common\logic\admin\Reviews;
use app\common\logic\home\Relation;
//课程信息
class MytaskController extends RestUserBaseController
{
	
	public function index(MytaskModel $Mytask){
		$param = $this->request->param();
		//已完成
	    $up_data['status'] = 3;
	    $up_data['com_time'] = date("Y-m-d H:i:s");
		$rs = MytaskModel::where('id',$param['taskid'])->where('uid',$this->userId)->update($up_data);
		$this->success('成功');
	}

	public function lists(){
        $data=MytaskModel::where('uid',$this->userId)->where('type',2)->where('status',3)->field('id,uid,create_time,update_time,name,description,com_time')->order('com_time desc')->select();
        if(!empty($data)){
        	$this->success("请求成功",$data);
        }else{
        	$this->error('失败');
        }
	}
	/**
	 * [设置课题名称]
	 * @param [type] $[id] [我的课题ID]
	 * @param [type] $[names] [名称]
	 */
	public function setNames(){
		$param = $this->request->param();
		$rs = MytaskModel::where('id',$param['id'])->where('uid',$this->userId)->update(['names'=>$param['names']]);
		if($rs){
			$this->success('成功');
		}else{
			$this->error('失败');
		}
	}

	public function checkMytaskId(){
		$param = $this->request->param();
		$rs = MytaskModel::where('id',$param['mytask_id'])->where('uid',$this->userId)->value('id');
		if($rs){
			$this->success('成功');
		}else{
			$this->error('失败');
		}
	}

	/**
	 * [根据课题获取等级]
	 * @param [type] $[mytask_id] [课题ID]
	 * @return [type] [description]
	 */
	public function getLevel(Relation $Relation){
		$param = $this->request->param();
		$rs = $Relation->checkGroup($param['mytask_id'],$this->userId);
		if($rs['code'] == 1){
			$this->success('组长',$rs['code']);
		}else{
			$this->error($rs['msg'],$rs['code']);
		}
	}

	/**
	 * [进行提审]
	 * @param [type] $[mytask_id] [课题ID]
	 * @return [type] [description]
	 */
	public function review(Reviews $Reviews,Relation $Relation){
		$param = $this->request->param();

		//判断是否是发放的课题
		$rs = $Relation->checkGroup($param['mytask_id'],$this->userId);

		if($rs['code'] == 0){
			$this->error('是组员身份，不能提审');
		}else if($rs['code'] == -1){
			$this->error('不是老师发放课题,无法提审',2);
		}
		
		//进行添加
		$rs = $Reviews->add($param['mytask_id'],$this->userId);
		if($rs['code'] == 1){
			$this->success('成功');
		}else{
			$this->error($rs['msg']);
		}

	}




	
}
